home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / lang / String.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  79.5 KB  |  2,095 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)String.java    1.112 98/09/23
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. import java.util.Hashtable;
  18. import java.util.Locale;
  19. import java.util.Comparator;
  20. import sun.io.ByteToCharConverter;
  21. import sun.io.CharToByteConverter;
  22. import java.io.CharConversionException;
  23. import java.io.UnsupportedEncodingException;
  24. import java.io.ObjectStreamClass;
  25. import java.io.ObjectStreamField;
  26. import java.lang.ref.SoftReference;
  27.  
  28. /**
  29.  * The <code>String</code> class represents character strings. All 
  30.  * string literals in Java programs, such as <code>"abc"</code>, are 
  31.  * implemented as instances of this class. 
  32.  * <p>
  33.  * Strings are constant; their values cannot be changed after they 
  34.  * are created. String buffers support mutable strings.
  35.  * Because String objects are immutable they can be shared. For example:
  36.  * <p><blockquote><pre>
  37.  *     String str = "abc";
  38.  * </pre></blockquote><p>
  39.  * is equivalent to:
  40.  * <p><blockquote><pre>
  41.  *     char data[] = {'a', 'b', 'c'};
  42.  *     String str = new String(data);
  43.  * </pre></blockquote><p>
  44.  * Here are some more examples of how strings can be used:
  45.  * <p><blockquote><pre>
  46.  *     System.out.println("abc");
  47.  *     String cde = "cde";
  48.  *     System.out.println("abc" + cde);
  49.  *     String c = "abc".substring(2,3);
  50.  *     String d = cde.substring(1, 2);
  51.  * </pre></blockquote>
  52.  * <p>
  53.  * The class <code>String</code> includes methods for examining 
  54.  * individual characters of the sequence, for comparing strings, for 
  55.  * searching strings, for extracting substrings, and for creating a 
  56.  * copy of a string with all characters translated to uppercase or to 
  57.  * lowercase. 
  58.  * <p>
  59.  * The Java language provides special support for the string 
  60.  * concatentation operator ( + ), and for conversion of 
  61.  * other objects to strings. String concatenation is implemented 
  62.  * through the <code>StringBuffer</code> class and its 
  63.  * <code>append</code> method.
  64.  * String conversions are implemented through the method 
  65.  * <code>toString</code>, defined by <code>Object</code> and 
  66.  * inherited by all classes in Java. For additional information on 
  67.  * string concatenation and conversion, see Gosling, Joy, and Steele, 
  68.  * <i>The Java Language Specification</i>. 
  69.  *
  70.  * @author  Lee Boynton
  71.  * @author  Arthur van Hoff
  72.  * @version 1.112, 09/23/98
  73.  * @see     java.lang.Object#toString()
  74.  * @see     java.lang.StringBuffer
  75.  * @see     java.lang.StringBuffer#append(boolean)
  76.  * @see     java.lang.StringBuffer#append(char)
  77.  * @see     java.lang.StringBuffer#append(char[])
  78.  * @see     java.lang.StringBuffer#append(char[], int, int)
  79.  * @see     java.lang.StringBuffer#append(double)
  80.  * @see     java.lang.StringBuffer#append(float)
  81.  * @see     java.lang.StringBuffer#append(int)
  82.  * @see     java.lang.StringBuffer#append(long)
  83.  * @see     java.lang.StringBuffer#append(java.lang.Object)
  84.  * @see     java.lang.StringBuffer#append(java.lang.String)
  85.  * @since   JDK1.0
  86.  */
  87. public final
  88. class String implements java.io.Serializable, Comparable {
  89.     /** The value is used for character storage. */
  90.     private char value[];
  91.  
  92.     /** The offset is the first index of the storage that is used. */
  93.     private int offset;
  94.  
  95.     /** The count is the number of characters in the String. */
  96.     private int count;
  97.  
  98.     /** The cached converter for each thread. 
  99.      *  Note: These are declared null to minimize the classes
  100.      *  that String must depend on during initialization
  101.      */
  102.     private static ThreadLocal btcConverter = null;
  103.     private static ThreadLocal ctbConverter = null;
  104.  
  105.     /**
  106.      * Returns a <code>ByteToCharConverter</code> that uses the specified
  107.      * encoding. For efficiency a cache is maintained that holds the last
  108.      * used converter.
  109.      *
  110.      * @param  enc The name of a character encoding
  111.      * @return     ByteToCharConverter for the specified encoding.
  112.      * @exception  UnsupportedEncodingException
  113.      *             If the named encoding is not supported
  114.      * @since      JDK1.2
  115.      */
  116.     private static ByteToCharConverter getBTCConverter(String encoding)
  117.                                   throws UnsupportedEncodingException {
  118.         ByteToCharConverter btc = null;
  119.         if (btcConverter == null)
  120.             btcConverter = new ThreadLocal();
  121.         SoftReference ref = (SoftReference)(btcConverter.get());
  122.         if (ref==null || (btc = (ByteToCharConverter)ref.get())==null ||
  123.                           !encoding.equals(btc.getCharacterEncoding())) {
  124.             btc = ByteToCharConverter.getConverter(encoding);
  125.             btcConverter.set(new SoftReference(btc));
  126.         } else {
  127.             btc.reset();
  128.         }
  129.         return btc;
  130.     }
  131.  
  132.     /**
  133.      * Returns a <code>CharToByteConverter</code> that uses the specified
  134.      * encoding. For efficiency a cache is maintained that holds the last
  135.      * used converter.
  136.      *
  137.      * @param  enc The name of a character encoding
  138.      * @return     CharToByteConverter for the specified encoding.
  139.      * @exception  UnsupportedEncodingException
  140.      *             If the named encoding is not supported
  141.      * @since      JDK1.2
  142.      */
  143.     private static CharToByteConverter getCTBConverter(String encoding)
  144.                                   throws UnsupportedEncodingException {
  145.         CharToByteConverter ctb = null;
  146.         if (ctbConverter == null)
  147.             ctbConverter = new ThreadLocal();
  148.         SoftReference ref = (SoftReference)(ctbConverter.get());
  149.         if (ref==null || (ctb = (CharToByteConverter)ref.get())==null ||
  150.                           !encoding.equals(ctb.getCharacterEncoding())) {
  151.             ctb = CharToByteConverter.getConverter(encoding);
  152.             ctbConverter.set(new SoftReference(ctb));
  153.         } else {
  154.             ctb.reset();
  155.         }
  156.         return ctb;
  157.     }
  158.  
  159.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  160.     private static final long serialVersionUID = -6849794470754667710L;
  161.  
  162.     /**
  163.      * Class String is special cased within the Serialization Stream Protocol. 
  164.      *
  165.      * A String instance is written intially into an ObjectOutputStream in the 
  166.      * following format:
  167.      * <pre>
  168.      *      <code>TC_STRING</code> (utf String)
  169.      * </pre>
  170.      * The String is written by method <code>DataOutput.writeUTF</code>. 
  171.      * A new handle is generated to  refer to all future references to the
  172.      * string instance within the stream.
  173.      */
  174.     private static final ObjectStreamField[] serialPersistentFields = 
  175.     ObjectStreamClass.NO_FIELDS;
  176.  
  177.     /**
  178.      * Initializes a newly created <code>String</code> object so that it 
  179.      * represents an empty character sequence. 
  180.      */
  181.     public String() {
  182.     value = new char[0];
  183.     }
  184.  
  185.     /**
  186.      * Initializes a newly created <code>String</code> object so that it 
  187.      * represents the same sequence of characters as the argument; in other 
  188.      * words, the newly created string is a copy of the argument string. 
  189.      *
  190.      * @param   value   a <code>String</code>.
  191.      */
  192.     public String(String value) {
  193.     count = value.length();
  194.     this.value = new char[count];
  195.     value.getChars(0, count, this.value, 0);
  196.     }
  197.  
  198.     /**
  199.      * Allocates a new <code>String</code> so that it represents the 
  200.      * sequence of characters currently contained in the character array 
  201.      * argument. The contents of the character array are copied; subsequent 
  202.      * modification of the character array does not affect the newly created 
  203.      * string. 
  204.      *
  205.      * @param  value   the initial value of the string.
  206.      * @throws NullPointerException if <code>value</code> is <code>null</code>.
  207.      */
  208.     public String(char value[]) {
  209.     this.count = value.length;
  210.     this.value = new char[count];
  211.     System.arraycopy(value, 0, this.value, 0, count);
  212.     }
  213.  
  214.     /**
  215.      * Allocates a new <code>String</code> that contains characters from 
  216.      * a subarray of the character array argument. The <code>offset</code> 
  217.      * argument is the index of the first character of the subarray and 
  218.      * the <code>count</code> argument specifies the length of the 
  219.      * subarray. The contents of the subarray are copied; subsequent 
  220.      * modification of the character array does not affect the newly 
  221.      * created string. 
  222.      *
  223.      * @param      value    array that is the source of characters.
  224.      * @param      offset   the initial offset.
  225.      * @param      count    the length.
  226.      * @exception  IndexOutOfBoundsException  if the <code>offset</code>
  227.      *               and <code>count</code> arguments index characters outside
  228.      *               the bounds of the <code>value</code> array.
  229.      * @exception NullPointerException if <code>value</code> is 
  230.      *               <code>null</code>.
  231.      */
  232.     public String(char value[], int offset, int count) {
  233.     if (offset < 0) {
  234.         throw new StringIndexOutOfBoundsException(offset);
  235.     }
  236.     if (count < 0) {
  237.         throw new StringIndexOutOfBoundsException(count);
  238.     }
  239.     // Note: offset or count might be near -1>>>1.
  240.     if (offset > value.length - count) {
  241.         throw new StringIndexOutOfBoundsException(offset + count);
  242.     }
  243.  
  244.     this.value = new char[count];
  245.     this.count = count;
  246.     System.arraycopy(value, offset, this.value, 0, count);
  247.     }
  248.  
  249.     /**
  250.      * Allocates a new <code>String</code> constructed from a subarray 
  251.      * of an array of 8-bit integer values. 
  252.      * <p>
  253.      * The <code>offset</code> argument is the index of the first byte 
  254.      * of the subarray, and the <code>count</code> argument specifies the 
  255.      * length of the subarray. 
  256.      * <p>
  257.      * Each <code>byte</code> in the subarray is converted to a 
  258.      * <code>char</code> as specified in the method above. 
  259.      *
  260.      * @deprecated This method does not properly convert bytes into characters.
  261.      * As of JDK 1.1, the preferred way to do this is via the
  262.      * <code>String</code> constructors that take a character-encoding name or
  263.      * that use the platform's default encoding.
  264.      *
  265.      * @param      ascii     the bytes to be converted to characters.
  266.      * @param      hibyte    the top 8 bits of each 16-bit Unicode character.
  267.      * @param      offset    the initial offset.
  268.      * @param      count     the length.
  269.      * @exception  IndexOutOfBoundsException  if the <code>offset</code>
  270.      *               or <code>count</code> argument is invalid.
  271.      * @exception NullPointerException if <code>ascii</code> is 
  272.      *                       <code>null</code>.
  273.      * @see        java.lang.String#String(byte[], int)
  274.      * @see        java.lang.String#String(byte[], int, int, java.lang.String)
  275.      * @see        java.lang.String#String(byte[], int, int)
  276.      * @see        java.lang.String#String(byte[], java.lang.String)
  277.      * @see        java.lang.String#String(byte[])
  278.      */
  279.     public String(byte ascii[], int hibyte, int offset, int count) {
  280.     if (offset < 0) {
  281.         throw new StringIndexOutOfBoundsException(offset);
  282.     }
  283.     if (count < 0) {
  284.         throw new StringIndexOutOfBoundsException(count);
  285.     }
  286.     // Note: offset or count might be near -1>>>1.
  287.     if (offset > ascii.length - count) {
  288.         throw new StringIndexOutOfBoundsException(offset + count);
  289.     }
  290.  
  291.     char value[] = new char[count];
  292.     this.count = count;
  293.     this.value = value;
  294.  
  295.     if (hibyte == 0) {
  296.         for (int i = count ; i-- > 0 ;) {
  297.         value[i] = (char) (ascii[i + offset] & 0xff);
  298.         }
  299.     } else {
  300.         hibyte <<= 8;
  301.         for (int i = count ; i-- > 0 ;) {
  302.         value[i] = (char) (hibyte | (ascii[i + offset] & 0xff));
  303.         }
  304.     }
  305.     }
  306.  
  307.     /**
  308.      * Allocates a new <code>String</code> containing characters 
  309.      * constructed from an array of 8-bit integer values. Each character 
  310.      * <i>c</i>in the resulting string is constructed from the 
  311.      * corresponding component <i>b</i> in the byte array such that:
  312.      * <p><blockquote><pre>
  313.      *     <b><i>c</i></b> == (char)(((hibyte & 0xff) << 8)
  314.      *                         | (<b><i>b</i></b> & 0xff))
  315.      * </pre></blockquote>
  316.      *
  317.      * @deprecated This method does not properly convert bytes into characters.
  318.      * As of JDK 1.1, the preferred way to do this is via the
  319.      * <code>String</code> constructors that take a character-encoding name or
  320.      * that use the platform's default encoding.
  321.      *
  322.      * @param      ascii    the bytes to be converted to characters.
  323.      * @param      hibyte   the top 8 bits of each 16-bit Unicode character.
  324.      * @exception NullPointerException If <code>ascii</code> is 
  325.      *                      <code>null</code>.
  326.      * @see        java.lang.String#String(byte[], int, int, java.lang.String)
  327.      * @see        java.lang.String#String(byte[], int, int)
  328.      * @see        java.lang.String#String(byte[], java.lang.String)
  329.      * @see        java.lang.String#String(byte[])
  330.      */
  331.     public String(byte ascii[], int hibyte) {
  332.     this(ascii, hibyte, 0, ascii.length);
  333.     }
  334.  
  335.    /**
  336.      * Construct a new <code>String</code> by converting the specified
  337.      * subarray of bytes using the specified character-encoding converter.  The
  338.      * length of the new <code>String</code> is a function of the encoding, and
  339.      * hence may not be equal to the length of the subarray.
  340.      *
  341.      * @param  bytes   The bytes to be converted into characters
  342.      * @param  offset  Index of the first byte to convert
  343.      * @param  length  Number of bytes to convert
  344.      * @param  btc     A ByteToCharConverter
  345.      * @exception  IndexOutOfBoundsException  if the <code>offset</code>
  346.      *               and <code>count</code> arguments index characters outside
  347.      *               the bounds of the <code>value</code> array.
  348.      */
  349.     private String(byte bytes[], int offset, int length,
  350.            ByteToCharConverter btc)
  351.     {
  352.         if (length < 0)
  353.             throw new StringIndexOutOfBoundsException("length must be >= 0");
  354.         if (offset < 0)
  355.             throw new StringIndexOutOfBoundsException("offset must be >= 0");
  356.         if (offset > bytes.length-length)
  357.             throw new StringIndexOutOfBoundsException(offset + count);
  358.  
  359.     int estCount = btc.getMaxCharsPerByte() * length;
  360.     value = new char[estCount];
  361.  
  362.         try {
  363.         count = btc.convert(bytes, offset, offset+length,
  364.                 value, 0, estCount);
  365.         count += btc.flush(value, btc.nextCharIndex(), estCount);
  366.     } catch (CharConversionException x) {
  367.         count = btc.nextCharIndex();
  368.     }
  369.  
  370.     if (count < estCount) {
  371.         // A multi-byte format was used:  Trim the char array.
  372.         char[] trimValue = new char[count];
  373.         System.arraycopy(value, 0, trimValue, 0, count);
  374.         value = trimValue;
  375.     }
  376.     }
  377.  
  378.  
  379.     /**
  380.      * Construct a new <code>String</code> by converting the specified
  381.      * subarray of bytes using the specified character encoding.  The length of
  382.      * the new <code>String</code> is a function of the encoding, and hence may
  383.      * not be equal to the length of the subarray.
  384.      *
  385.      * @param  bytes   The bytes to be converted into characters
  386.      * @param  offset  Index of the first byte to convert
  387.      * @param  length  Number of bytes to convert
  388.      * @param  enc     The name of a character encoding
  389.      *
  390.      * @exception  UnsupportedEncodingException
  391.      *             If the named encoding is not supported
  392.      *             IndexOutOfBoundsException  if the <code>offset</code>
  393.      *               and <code>count</code> arguments index characters outside
  394.      *               the bounds of the <code>value</code> array.
  395.      * @since      JDK1.1
  396.      */
  397.     public String(byte bytes[], int offset, int length, String enc)
  398.     throws UnsupportedEncodingException
  399.     {
  400.     this(bytes, offset, length, getBTCConverter(enc));
  401.     }
  402.  
  403.     /**
  404.      * Construct a new <code>String</code> by converting the specified array
  405.      * of bytes using the specified character encoding.  The length of the new
  406.      * <code>String</code> is a function of the encoding, and hence may not be
  407.      * equal to the length of the byte array.
  408.      *
  409.      * @param  bytes   The bytes to be converted into characters
  410.      * @param  enc     A character-encoding name
  411.      *
  412.      * @exception  UnsupportedEncodingException
  413.      *             If the named encoding is not supported
  414.      * @since      JDK1.1
  415.      */
  416.     public String(byte bytes[], String enc)
  417.     throws UnsupportedEncodingException
  418.     {
  419.     this(bytes, 0, bytes.length, enc);
  420.     }
  421.  
  422.     /**
  423.      * Construct a new <code>String</code> by converting the specified
  424.      * subarray of bytes using the platform's default character encoding.  The
  425.      * length of the new <code>String</code> is a function of the encoding, and
  426.      * hence may not be equal to the length of the subarray.
  427.      *
  428.      * @param  bytes   The bytes to be converted into characters
  429.      * @param  offset  Index of the first byte to convert
  430.      * @param  length  Number of bytes to convert
  431.      * @since  JDK1.1
  432.      */
  433.     public String(byte bytes[], int offset, int length) {
  434.     this(bytes, offset, length, ByteToCharConverter.getDefault());
  435.     }
  436.  
  437.     /**
  438.      * Construct a new <code>String</code> by converting the specified array
  439.      * of bytes using the platform's default character encoding.  The length of
  440.      * the new <code>String</code> is a function of the encoding, and hence may
  441.      * not be equal to the length of the byte array.
  442.      *
  443.      * @param  bytes   The bytes to be converted into characters
  444.      * @since  JDK1.1
  445.      */
  446.     public String(byte bytes[]) {
  447.     this(bytes, 0, bytes.length, ByteToCharConverter.getDefault());
  448.     }
  449.  
  450.     /**
  451.      * Allocates a new string that contains the sequence of characters 
  452.      * currently contained in the string buffer argument. The contents of 
  453.      * the string buffer are copied; subsequent modification of the string 
  454.      * buffer does not affect the newly created string.
  455.      *
  456.      * @param   buffer   a <code>StringBuffer</code>.
  457.      * @throws NullPointerException If <code>buffer</code> is 
  458.      * <code>null</code>.
  459.      */
  460.     public String (StringBuffer buffer) { 
  461.     synchronized(buffer) { 
  462.         buffer.setShared();
  463.         this.value = buffer.getValue();
  464.         this.offset = 0;
  465.         this.count = buffer.length();
  466.     }
  467.     }
  468.     
  469.     // Private constructor which shares value array for speed.
  470.     private String(int offset, int count, char value[]) {
  471.     this.value = value;
  472.     this.offset = offset;
  473.     this.count = count;
  474.     }
  475.  
  476.     /**
  477.      * Returns the length of this string.
  478.      * The length is equal to the number of 16-bit
  479.      * Unicode characters in the string.
  480.      *
  481.      * @return  the length of the sequence of characters represented by this
  482.      *          object.
  483.      */
  484.     public int length() {
  485.     return count;
  486.     }
  487.  
  488.     /**
  489.      * Returns the character at the specified index. An index ranges
  490.      * from <code>0</code> to <code>length() - 1</code>. The first character 
  491.      * of the sequence is at index <code>0</code>, the next at index 
  492.      * <code>1</code>, and so on, as for array indexing.
  493.      *
  494.      * @param      index   the index of the character.
  495.      * @return     the character at the specified index of this string.
  496.      *             The first character is at index <code>0</code>.
  497.      * @exception  IndexOutOfBoundsException  if the <code>index</code> 
  498.      *             argument is negative or not less than the length of this 
  499.      *             string.
  500.      */
  501.     public char charAt(int index) {
  502.     if ((index < 0) || (index >= count)) {
  503.         throw new StringIndexOutOfBoundsException(index);
  504.     }
  505.     return value[index + offset];
  506.     }
  507.  
  508.     /**
  509.      * Copies characters from this string into the destination character 
  510.      * array. 
  511.      * <p>
  512.      * The first character to be copied is at index <code>srcBegin</code>; 
  513.      * the last character to be copied is at index <code>srcEnd-1</code> 
  514.      * (thus the total number of characters to be copied is 
  515.      * <code>srcEnd-srcBegin</code>). The characters are copied into the 
  516.      * subarray of <code>dst</code> starting at index <code>dstBegin</code> 
  517.      * and ending at index: 
  518.      * <p><blockquote><pre>
  519.      *     dstbegin + (srcEnd-srcBegin) - 1
  520.      * </pre></blockquote>
  521.      *
  522.      * @param      srcBegin   index of the first character in the string
  523.      *                        to copy.
  524.      * @param      srcEnd     index after the last character in the string
  525.      *                        to copy.
  526.      * @param      dst        the destination array.
  527.      * @param      dstBegin   the start offset in the destination array.
  528.      * @exception IndexOutOfBoundsException If any of the following 
  529.      *            is true:
  530.      *            <ul><li><code>srcBegin</code> is negative.
  531.      *            <li><code>srcBegin</code> is greater than <code>srcEnd</code>
  532.      *            <li><code>srcEnd</code> is greater than the length of this 
  533.      *                string
  534.      *            <li><code>dstBegin</code> is negative
  535.      *            <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than 
  536.      *                <code>dst.length</code></ul>
  537.      * @exception NullPointerException if <code>dst</code> is <code>null</code>
  538.      */
  539.     public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
  540.     if (srcBegin < 0) {
  541.         throw new StringIndexOutOfBoundsException(srcBegin);
  542.     } 
  543.     if (srcEnd > count) {
  544.         throw new StringIndexOutOfBoundsException(srcEnd);
  545.     } 
  546.     if (srcBegin > srcEnd) {
  547.         throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
  548.     }
  549.     System.arraycopy(value, offset + srcBegin, dst, dstBegin, 
  550.              srcEnd - srcBegin);
  551.     }
  552.  
  553.     /**
  554.      * Copies characters from this string into the destination byte 
  555.      * array. Each byte receives the 8 low-order bits of the 
  556.      * corresponding character. The eight high-order bits of each character 
  557.      * are not copied and do not participate in the transfer in any way.
  558.      * <p>
  559.      * The first character to be copied is at index <code>srcBegin</code>; 
  560.      * the last character to be copied is at index <code>srcEnd-1</code>. 
  561.      * The total number of characters to be copied is 
  562.      * <code>srcEnd-srcBegin</code>. The characters, converted to bytes, 
  563.      * are copied into the subarray of <code>dst</code> starting at index 
  564.      * <code>dstBegin</code> and ending at index: 
  565.      * <p><blockquote><pre>
  566.      *     dstbegin + (srcEnd-srcBegin) - 1
  567.      * </pre></blockquote>
  568.      *
  569.      * @deprecated This method does not properly convert characters into bytes.
  570.      * As of JDK 1.1, the preferred way to do this is via the
  571.      * <code>getBytes(String enc)</code> method, which takes a
  572.      * character-encoding name, or the <code>getBytes()</code> method, which
  573.      * uses the platform's default encoding.
  574.      *
  575.      * @param      srcBegin   index of the first character in the string
  576.      *                        to copy.
  577.      * @param      srcEnd     index after the last character in the string
  578.      *                        to copy.
  579.      * @param      dst        the destination array.
  580.      * @param      dstBegin   the start offset in the destination array.
  581.      * @exception IndexOutOfBoundsException if any of the following 
  582.      *            is true:
  583.      *           <ul<li><code>srcBegin</code> is negative 
  584.      *           <li><code>srcBegin</code> is greater than <code>srcEnd</code> 
  585.      *           <li><code>srcEnd</code> is greater than the length of this 
  586.      *            String 
  587.      *           <li><code>dstBegin</code> is negative 
  588.      *           <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than 
  589.      *            <code>dst.length</code> 
  590.      * @exception NullPointerException if <code>dst</code> is <code>null</code>
  591.      */
  592.     public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
  593.     if (srcBegin < 0) {
  594.         throw new StringIndexOutOfBoundsException(srcBegin);
  595.     } 
  596.     if (srcEnd > count) {
  597.         throw new StringIndexOutOfBoundsException(srcEnd);
  598.     } 
  599.     if (srcBegin > srcEnd) {
  600.         throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
  601.     }
  602.      int j = dstBegin;
  603.      int n = offset + srcEnd;
  604.      int i = offset + srcBegin;
  605.     char[] val = value;   /* avoid getfield opcode */
  606.  
  607.      while (i < n) {
  608.          dst[j++] = (byte)val[i++];
  609.      }
  610.     }
  611.  
  612.     /**
  613.      * Apply the specified character-encoding converter to this String,
  614.      * storing the resulting bytes into a new byte array.
  615.      *
  616.      * @param  ctb  A CharToByteConverter
  617.      * @return      The resultant byte array
  618.      */
  619.     private byte[] getBytes(CharToByteConverter ctb) {
  620.     ctb.reset();
  621.     int estLength = ctb.getMaxBytesPerChar() * count;
  622.     byte[] result = new byte[estLength];
  623.     int length = 0;
  624.     try { 
  625.         length += ctb.convertAny(value, offset, (offset + count),
  626.                      result, 0, estLength);
  627.         length += ctb.flushAny(result, ctb.nextByteIndex(), estLength);
  628.     } catch (CharConversionException e) {
  629.         throw new InternalError("Converter malfunction: " +
  630.                     ctb.getClass().getName());
  631.     }
  632.  
  633.  
  634.     if (length < estLength) {
  635.         // A short format was used:  Trim the byte array.
  636.         byte[] trimResult = new byte[length];
  637.         System.arraycopy(result, 0, trimResult, 0, length);
  638.         return trimResult;
  639.     }
  640.     else {
  641.         return result;
  642.     }
  643.     }
  644.  
  645.     /**
  646.      * Convert this <code>String</code> into bytes according to the specified
  647.      * character encoding, storing the result into a new byte array.
  648.      *
  649.      * @param  enc  A character-encoding name
  650.      * @return      The resultant byte array
  651.      *
  652.      * @exception  UnsupportedEncodingException
  653.      *             If the named encoding is not supported
  654.      * @since      JDK1.1
  655.      */
  656.     public byte[] getBytes(String enc)
  657.     throws UnsupportedEncodingException
  658.     {
  659.     return getBytes(getCTBConverter(enc));
  660.     }
  661.  
  662.     /**
  663.      * Convert this <code>String</code> into bytes according to the platform's
  664.      * default character encoding, storing the result into a new byte array.
  665.      *
  666.      * @return  the resultant byte array.
  667.      * @since   JDK1.1
  668.      */
  669.     public byte[] getBytes() {
  670.     return getBytes(CharToByteConverter.getDefault());
  671.     }
  672.  
  673.     /**
  674.      * Compares this string to the specified object.
  675.      * The result is <code>true</code> if and only if the argument is not 
  676.      * <code>null</code> and is a <code>String</code> object that represents 
  677.      * the same sequence of characters as this object. 
  678.      *
  679.      * @param   anObject   the object to compare this <code>String</code>
  680.      *                     against.
  681.      * @return  <code>true</code> if the <code>String </code>are equal;
  682.      *          <code>false</code> otherwise.
  683.      * @see     java.lang.String#compareTo(java.lang.String)
  684.      * @see     java.lang.String#equalsIgnoreCase(java.lang.String)
  685.      */
  686.     public boolean equals(Object anObject) {
  687.     if (this == anObject) {
  688.         return true;
  689.     }
  690.     if ((anObject != null) && (anObject instanceof String)) {
  691.         String anotherString = (String)anObject;
  692.         int n = count;
  693.         if (n == anotherString.count) {
  694.         char v1[] = value;
  695.         char v2[] = anotherString.value;
  696.         int i = offset;
  697.         int j = anotherString.offset;
  698.         while (n-- != 0) {
  699.             if (v1[i++] != v2[j++]) {
  700.             return false;
  701.             }
  702.         }
  703.         return true;
  704.         }
  705.     }
  706.     return false;
  707.     }
  708.  
  709.     /**
  710.      * Compares this <code>String</code> to another <code>String</code>,
  711.      * ignoring case considerations.  Two strings are considered equal
  712.      * ignoring case if they are of the same length, and corresponding
  713.      * characters in the two strings are equal ignoring case.
  714.      * <p>
  715.      * Two characters <code>c1</code> and <code>c2</code> are considered
  716.      * the same, ignoring case if at least one of the following is true:
  717.      * <ul><li>The two characters are the same (as compared by the 
  718.      * <code>==</code> operator).
  719.      * <li>Applying the method {@link java.lang.Character#toUppercase(char)} 
  720.      * to each character produces the same result.
  721.      * <li>Applying the method {@link java.lang.Character#toLowercase(char) 
  722.      * to each character produces the same result.</ul>
  723.      *
  724.      * @param   anotherString   the <code>String</code> to compare this
  725.      *                          <code>String</code> against.
  726.      * @return  <code>true</code> if the argument is not <code>null</code> 
  727.      *          and the <code>String</code>s are equal,
  728.      *          ignoring case; <code>false</code> otherwise.
  729.      * @see     #equals(Object)
  730.      * @see     java.lang.Character#toLowerCase(char)
  731.      * @see java.lang.Character#toUpperCase(char)
  732.      */
  733.     public boolean equalsIgnoreCase(String anotherString) {
  734.     return (anotherString != null) && (anotherString.count == count) &&
  735.         regionMatches(true, 0, anotherString, 0, count);
  736.     }
  737.  
  738.     /**
  739.      * Compares two strings lexicographically. 
  740.      * The comparison is based on the Unicode value of each character in
  741.      * the strings. The character sequence represented by this 
  742.      * <code>String</code> object is compared lexicographically to the 
  743.      * character sequence represented by the argument string. The result is 
  744.      * a negative integer if this <code>String</code> object 
  745.      * lexicographically precedes the argument string. The result is a 
  746.      * positive integer if this <code>String</code> object lexicographically 
  747.      * follows the argument string. The result is zero if the strings
  748.      * are equal; <code>compareTo</code> returns <code>0</code> exactly when 
  749.      * the {@link #equals(Object)} method would return <code>true</code>. 
  750.      * <p>
  751.      * This is the definition of lexicographic ordering. If two strings are 
  752.      * different, then either they have different characters at some index 
  753.      * that is a valid index for both strings, or their lengths are different, 
  754.      * or both. If they have different characters at one or more index 
  755.      * positions, let <i>k</i> be the smallest such index; then the string
  756.      * whose character at position <i>k</i> has the smaller value, as 
  757.      * determined by using the < operator, lexicographically precedes the 
  758.      * other string. In this case, <code>compareTo</code> returns the 
  759.      * difference of the two character values at position <code>k</code> in 
  760.      * the two string -- that is, the value:
  761.      * <blockquote><pre>
  762.      * this.charAt(k)-anotherString.charAt(k)
  763.      * </pre></blockquote>
  764.      * If there is no index position at which they differ, then the shorter 
  765.      * string lexicographically precedes the longer string. In this case, 
  766.      * <code>compareTo</code> returns the difference of the lengths of the 
  767.      * strings -- that is, the value: 
  768.      * <blockquote><pre>
  769.      * this.length()-anotherString.length()
  770.      * </pre></blockquote>
  771.      *
  772.      * @param   anotherString   the <code>String</code> to be compared.
  773.      * @return  the value <code>0</code> if the argument string is equal to
  774.      *          this string; a value less than <code>0</code> if this string
  775.      *          is lexicographically less than the string argument; and a
  776.      *          value greater than <code>0</code> if this string is
  777.      *          lexicographically greater than the string argument.
  778.      * @exception java.lang.NullPointerException if <code>anotherString</code> 
  779.      *          is <code>null</code>.
  780.      */
  781.     public int compareTo(String anotherString) {
  782.     int len1 = count;
  783.     int len2 = anotherString.count;
  784.     int n = Math.min(len1, len2);
  785.     char v1[] = value;
  786.     char v2[] = anotherString.value;
  787.     int i = offset;
  788.     int j = anotherString.offset;
  789.  
  790.     while (n-- != 0) {
  791.         char c1 = v1[i++];
  792.         char c2 = v2[j++];
  793.         if (c1 != c2) {
  794.         return c1 - c2;
  795.         }
  796.     }
  797.     return len1 - len2;
  798.     }
  799.  
  800.     /**
  801.      * Compares this String to another Object.  If the Object is a String,
  802.      * this function behaves like <code>compareTo(String)</code>.  Otherwise,
  803.      * it throws a <code>ClassCastException</code> (as Strings are comparable
  804.      * only to other Strings).
  805.      *
  806.      * @param   o the <code>Object</code> to be compared.
  807.      * @return  the value <code>0</code> if the argument is a string
  808.      *        lexicographically equal to this string; a value less than
  809.      *        <code>0</code> if the argument is a string lexicographically 
  810.      *        greater than this string; and a value greater than
  811.      *        <code>0</code> if the argument is a string lexicographically
  812.      *        less than this string.
  813.      * @exception <code>ClassCastException</code> if the argument is not a
  814.      *          <code>String</code>. 
  815.      * @see     java.lang.Comparable
  816.      * @since   JDK1.2
  817.      */
  818.     public int compareTo(Object o) {
  819.     return compareTo((String)o);
  820.     }
  821.  
  822.     /**
  823.      * Returns a Comparator that orders <code>String</code> objects as by
  824.      * <code>compareToIgnoreCase</code>.
  825.      * <p>
  826.      * Note that this Comparator does <em>not</em> take locale into account,
  827.      * and will result in an unsatisfactory ordering for certain locales.
  828.      * The java.text package provides <em>Collators</em> to allow
  829.      * locale-sensitive ordering.
  830.      *
  831.      * @return  Comparator for case insensitive comparison of strings
  832.      * @see     java.text.Collator#compare(String, String)
  833.      * @since   JDK1.2
  834.      */
  835.     public static final Comparator CASE_INSENSITIVE_ORDER = new Comparator() {
  836.         public int compare(Object o1, Object o2) {
  837.             String s1 = (String) o1;
  838.             String s2 = (String) o2;
  839.             int n1=s1.length(), n2=s2.length();
  840.             for (int i1=0, i2=0; i1<n1 && i2<n2; i1++, i2++) {
  841.                 char c1 = s1.charAt(i1);
  842.                 char c2 = s2.charAt(i2);
  843.                 if (c1 != c2) {
  844.                     c1 = Character.toUpperCase(c1);
  845.                     c2 = Character.toUpperCase(c2);
  846.                     if (c1 != c2) {
  847.                         c1 = Character.toLowerCase(c1);
  848.                         c2 = Character.toLowerCase(c2);
  849.                         if (c1 != c2)
  850.                             return c1 - c2;
  851.                     }
  852.                 }
  853.             }
  854.             return n1 - n2;
  855.         }
  856.     };
  857.  
  858.     /**
  859.      * Compares two strings lexicographically, ignoring case considerations.
  860.      * This method returns an integer whose sign is that of
  861.      * <code>this.toUpperCase().toLowerCase().compareTo(
  862.      * str.toUpperCase().toLowerCase())</code>.
  863.      * <p>
  864.      * Note that this method does <em>not</em> take locale into account,
  865.      * and will result in an unsatisfactory ordering for certain locales.
  866.      * The java.text package provides <em>collators</em> to allow
  867.      * locale-sensitive ordering.
  868.      *
  869.      * @param   str   the <code>String</code> to be compared.
  870.      * @return  a negative integer, zero, or a positive integer as the
  871.      *        the specified String is greater than, equal to, or less
  872.      *        than this String, ignoring case considerations.
  873.      * @see     java.text.Collator#compare(String, String)
  874.      * @since   JDK1.2
  875.      */
  876.     public int compareToIgnoreCase(String str) {
  877.         return CASE_INSENSITIVE_ORDER.compare(this, str);
  878.     }
  879.  
  880.     /**
  881.      * Tests if two string regions are equal. 
  882.      * <p>
  883.      * A substring of this <tt>String</tt> object is compared to a substring 
  884.      * of the argument other. The result is true if these substrings 
  885.      * represent identical character sequences. The substring of this 
  886.      * <tt>String</tt> object to be compared begins at index <tt>toffset</tt> 
  887.      * and has length <tt>len</tt>. The substring of other to be compared 
  888.      * begins at index <tt>ooffset</tt> and has length <tt>len</tt>. The 
  889.      * result is <tt>false</tt> if and only if at least one of the following 
  890.      * is true: 
  891.      * <ul><li><tt>toffset</tt> is negative. 
  892.      * <li><tt>ooffset</tt> is negative. 
  893.      * <li><tt>toffset+len</tt> is greater than the length of this 
  894.      * <tt>String</tt> object. 
  895.      * <li><tt>ooffset+len</tt> is greater than the length of the other 
  896.      * argument. 
  897.      * <li>There is some nonnegative integer <i>k</i> less than <tt>len</tt> 
  898.      * such that: 
  899.      * <tt>this.charAt(toffset+<i>k</i>) != other.charAt(ooffset+<i>k</i>)</tt> 
  900.      * </u>
  901.      *
  902.      * @param   toffset   the starting offset of the subregion in this string.
  903.      * @param   other     the string argument.
  904.      * @param   ooffset   the starting offset of the subregion in the string
  905.      *                    argument.
  906.      * @param   len       the number of characters to compare.
  907.      * @return  <code>true</code> if the specified subregion of this string
  908.      *          exactly matches the specified subregion of the string argument;
  909.      *          <code>false</code> otherwise.
  910.      * @exception java.lang.NullPointerException if <tt>other</tt> is 
  911.      *          <tt>null</tt>.
  912.      */
  913.     public boolean regionMatches(int toffset, String other, int ooffset, 
  914.                  int len) {
  915.     char ta[] = value;
  916.     int to = offset + toffset;
  917.     int tlim = offset + count;
  918.     char pa[] = other.value;
  919.     int po = other.offset + ooffset;
  920.     // Note: toffset, ooffset, or len might be near -1>>>1.
  921.     if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len)
  922.         || (ooffset > (long)other.count - len)) {
  923.         return false;
  924.     }
  925.     while (len-- > 0) {
  926.         if (ta[to++] != pa[po++]) {
  927.             return false;
  928.         }
  929.     }
  930.     return true;
  931.     }
  932.  
  933.     /**
  934.      * Tests if two string regions are equal. 
  935.      * <p>
  936.      * A substring of this <tt>String</tt> object is compared to a substring 
  937.      * of the argument <tt>other</tt>. The result is <tt>true</tt> if these 
  938.      * substrings represent character sequences that are the same, ignoring 
  939.      * case if and only if <tt>ignoreCase</tt> is true. The substring of
  940.      * this <tt>String</tt> object to be compared begins at index 
  941.      * <tt>toffset</tt> and has length <tt>len</tt>. The substring of 
  942.      * <tt>other</tt> to be compared begins at index <tt>ooffset</tt> and 
  943.      * has length <tt>len</tt>. The result is <tt>false</tt> if and only if 
  944.      * at least one of the following is true: 
  945.      * <ul><li><tt>toffset</tt> is negative. 
  946.      * <li><tt>ooffset</tt> is negative. 
  947.      * <li><tt>toffset+len</tt> is greater than the length of this 
  948.      * <tt>String</tt> object. 
  949.      * <li><tt>ooffset+len</tt> is greater than the length of the other 
  950.      * argument. 
  951.      * <li>There is some nonnegative integer <i>k</i> less than <tt>len</tt> 
  952.      * such that:
  953.      * <blockquote><pre>
  954.      * this.charAt(toffset+k) != other.charAt(ooffset+k) 
  955.      * </pre></blockquote>
  956.      * <li><tt>ignoreCase</tt> is <tt>true</tt> and there is some nonnegative 
  957.      * integer <i>k</i> less than <tt>len</tt> such that: 
  958.      * <blockquote><pre>
  959.      * Character.toLowerCase(this.charAt(toffset+k)) !=
  960.                Character.toLowerCase(other.charAt(ooffset+k))
  961.      * </pre></blockquote> 
  962.      * and: 
  963.      * <blockquote><pre>
  964.      * Character.toUpperCase(this.charAt(toffset+k)) !=
  965.      *         Character.toUpperCase(other.charAt(ooffset+k))
  966.      * </pre></blockquote>
  967.      * </ul>
  968.      *
  969.      * @param   ignoreCase   if <code>true</code>, ignore case when comparing
  970.      *                       characters.
  971.      * @param   toffset      the starting offset of the subregion in this
  972.      *                       string.
  973.      * @param   other        the string argument.
  974.      * @param   ooffset      the starting offset of the subregion in the string
  975.      *                       argument.
  976.      * @param   len          the number of characters to compare.
  977.      * @return  <code>true</code> if the specified subregion of this string
  978.      *          matches the specified subregion of the string argument;
  979.      *          <code>false</code> otherwise. Whether the matching is exact
  980.      *          or case insensitive depends on the <code>ignoreCase</code>
  981.      *          argument.
  982.      */
  983.     public boolean regionMatches(boolean ignoreCase,
  984.                          int toffset,
  985.                            String other, int ooffset, int len) {
  986.     char ta[] = value;
  987.     int to = offset + toffset;
  988.     int tlim = offset + count;
  989.     char pa[] = other.value;
  990.     int po = other.offset + ooffset;
  991.     // Note: toffset, ooffset, or len might be near -1>>>1.
  992.     if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len) ||
  993.             (ooffset > (long)other.count - len)) {
  994.         return false;
  995.     }
  996.     while (len-- > 0) {
  997.         char c1 = ta[to++];
  998.         char c2 = pa[po++];
  999.         if (c1 == c2)
  1000.         continue;
  1001.         if (ignoreCase) {
  1002.         // If characters don't match but case may be ignored,
  1003.         // try converting both characters to uppercase.
  1004.         // If the results match, then the comparison scan should
  1005.         // continue. 
  1006.         char u1 = Character.toUpperCase(c1);
  1007.         char u2 = Character.toUpperCase(c2);
  1008.         if (u1 == u2)
  1009.             continue;
  1010.         // Unfortunately, conversion to uppercase does not work properly
  1011.         // for the Georgian alphabet, which has strange rules about case
  1012.         // conversion.  So we need to make one last check before 
  1013.         // exiting.
  1014.         if (Character.toLowerCase(u1) == Character.toLowerCase(u2))
  1015.             continue;
  1016.         }
  1017.         return false;
  1018.     }
  1019.     return true;
  1020.     }
  1021.  
  1022.     /**
  1023.      * Tests if this string starts with the specified prefix beginning 
  1024.      * a specified index.
  1025.      *
  1026.      * @param   prefix    the prefix.
  1027.      * @param   toffset   where to begin looking in the string.
  1028.      * @return  <code>true</code> if the character sequence represented by the
  1029.      *          argument is a prefix of the substring of this object starting
  1030.      *          at index <code>toffset</code>; <code>false</code> otherwise. 
  1031.      *          The result is <code>false</code> if <code>toffset</code> is 
  1032.      *          negative or greater than the length of this 
  1033.      *          <code>String</code> object; otherwise the result is the same 
  1034.      *          as the result of the expression
  1035.      *          <pre>
  1036.      *          this.subString(toffset).startsWith(prefix)
  1037.      *          </pre>
  1038.      * @exception java.lang.NullPointerException if <code>prefix</code> is 
  1039.      *          <code>null</code>.
  1040.      */
  1041.     public boolean startsWith(String prefix, int toffset) {
  1042.     char ta[] = value;
  1043.     int to = offset + toffset;
  1044.     int tlim = offset + count;
  1045.     char pa[] = prefix.value;
  1046.     int po = prefix.offset;
  1047.     int pc = prefix.count;
  1048.     // Note: toffset might be near -1>>>1.
  1049.     if ((toffset < 0) || (toffset > count - pc)) {
  1050.         return false;
  1051.     }
  1052.     while (--pc >= 0) {
  1053.         if (ta[to++] != pa[po++]) {
  1054.             return false;
  1055.         }
  1056.     }
  1057.     return true;
  1058.     }
  1059.  
  1060.     /**
  1061.      * Tests if this string starts with the specified prefix.
  1062.      *
  1063.      * @param   prefix   the prefix.
  1064.      * @return  <code>true</code> if the character sequence represented by the
  1065.      *          argument is a prefix of the character sequence represented by
  1066.      *          this string; <code>false</code> otherwise.      
  1067.      *          Note also that <code>true</code> will be returned if the 
  1068.      *          argument is an empty string or is equal to this 
  1069.      *          <code>String</code> object as determined by the 
  1070.      *          {@link #equals(Object)} method.
  1071.      * @exception java.lang.NullPointerException if <code>prefix</code> is 
  1072.      *          <code>null</code>.
  1073.      * @since   JDK1. 0
  1074.      */
  1075.     public boolean startsWith(String prefix) {
  1076.     return startsWith(prefix, 0);
  1077.     }
  1078.  
  1079.     /**
  1080.      * Tests if this string ends with the specified suffix.
  1081.      *
  1082.      * @param   suffix   the suffix.
  1083.      * @return  <code>true</code> if the character sequence represented by the
  1084.      *          argument is a suffix of the character sequence represented by
  1085.      *          this object; <code>false</code> otherwise. Note that the 
  1086.      *          result will be <code>true</code> if the argument is the 
  1087.      *          empty string or is equal to this <code>String</code> object 
  1088.      *          as determined by the {@link #equals(Object)} method.
  1089.      * @exception java.lang.NullPointerException if <code>suffix</code> is 
  1090.      *          <code>null</code>.
  1091.      */
  1092.     public boolean endsWith(String suffix) {
  1093.     return startsWith(suffix, count - suffix.count);
  1094.     }
  1095.  
  1096.     /**
  1097.      * Returns a hashcode for this string. The hashcode for a 
  1098.      * <code>String</code> object is computed as
  1099.      * <blockquote><pre> 
  1100.      * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
  1101.      * </pre></blockquote>
  1102.      * using <code>int</code> arithmetic, where <code>s[i]</code> is the 
  1103.      * <i>i</i>th character of the string, <code>n</code> is the length of 
  1104.      * the string, and <code>^</code> indicates exponentiation. 
  1105.      * (The hash value of the empty string is zero.)
  1106.      *
  1107.      * @return  a hash code value for this object. 
  1108.      */
  1109.     public int hashCode() {
  1110.     int h = 0;
  1111.     int off = offset;
  1112.     char val[] = value;
  1113.     int len = count;
  1114.  
  1115.     for (int i = 0; i < len; i++)
  1116.         h = 31*h + val[off++];
  1117.  
  1118.     return h;
  1119.     }
  1120.  
  1121.     /**
  1122.      * Returns the index within this string of the first occurrence of the
  1123.      * specified character. If a character with value <code>ch</code> occurs 
  1124.      * in the character sequence represented by this <code>String</code> 
  1125.      * object, then the index of the first such occurrence is returned -- 
  1126.      * that is, the smallest value <i>k</i> such that: 
  1127.      * <blockquote><pre>
  1128.      * this.charAt(<i>k</i>) == ch
  1129.      * </pre></blockquote>
  1130.      * is <code>true</code>. If no such character occurs in this string, 
  1131.      * then <code>-1</code> is returned.
  1132.      *
  1133.      * @param   ch   a character.
  1134.      * @return  the index of the first occurrence of the character in the
  1135.      *          character sequence represented by this object, or
  1136.      *          <code>-1</code> if the character does not occur.
  1137.      */
  1138.     public int indexOf(int ch) {
  1139.     return indexOf(ch, 0);
  1140.     }
  1141.  
  1142.     /**
  1143.      * Returns the index within this string of the first occurrence of the
  1144.      * specified character, starting the search at the specified index.
  1145.      * <p>
  1146.      * If a character with value <code>ch</code> occurs in the character 
  1147.      * sequence represented by this <code>String</code> object at an index 
  1148.      * no smaller than <code>fromIndex</code>, then the index of the first
  1149.      * such occurrence is returned--that is, the smallest value <i>k</i> 
  1150.      * such that: 
  1151.      * <blockquote><pre>
  1152.      * (this.charAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex)
  1153.      * </pre></blockquote>
  1154.      * is true. If no such character occurs in this string at or after 
  1155.      * position <code>fromIndex</code>, then <code>-1</code> is returned.
  1156.      * <p>
  1157.      * There is no restriction on the value of <code>fromIndex</code>. If it 
  1158.      * is negative, it has the same effect as if it were zero: this entire 
  1159.      * string may be searched. If it is greater than the length of this 
  1160.      * string, it has the same effect as if it were equal to the length of 
  1161.      * this string: <code>-1</code> is returned.
  1162.      *
  1163.      * @param   ch          a character.
  1164.      * @param   fromIndex   the index to start the search from.
  1165.      * @return  the index of the first occurrence of the character in the
  1166.      *          character sequence represented by this object that is greater
  1167.      *          than or equal to <code>fromIndex</code>, or <code>-1</code>
  1168.      *          if the character does not occur.
  1169.      */
  1170.     public int indexOf(int ch, int fromIndex) {
  1171.     int max = offset + count;
  1172.     char v[] = value;
  1173.  
  1174.     if (fromIndex < 0) {
  1175.         fromIndex = 0;
  1176.     } else if (fromIndex >= count) {
  1177.         // Note: fromIndex might be near -1>>>1.
  1178.         return -1;
  1179.     }
  1180.     for (int i = offset + fromIndex ; i < max ; i++) {
  1181.         if (v[i] == ch) {
  1182.         return i - offset;
  1183.         }
  1184.     }
  1185.     return -1;
  1186.     }
  1187.  
  1188.     /**
  1189.      * Returns the index within this string of the last occurrence of the
  1190.      * specified character. That is, the index returned is the largest 
  1191.      * value <i>k</i> such that:
  1192.      * <blockquote><pre>
  1193.      * this.charAt(<i>k</i>) == ch
  1194.      * </pre></blockquote>
  1195.      * is true. 
  1196.      * The String is searched backwards starting at the last character. 
  1197.      *
  1198.      * @param   ch   a character.
  1199.      * @return  the index of the last occurrence of the character in the
  1200.      *          character sequence represented by this object, or
  1201.      *          <code>-1</code> if the character does not occur.
  1202.      */
  1203.     public int lastIndexOf(int ch) {
  1204.     return lastIndexOf(ch, count - 1);
  1205.     }
  1206.  
  1207.     /**
  1208.      * Returns the index within this string of the last occurrence of the
  1209.      * specified character, searching backward starting at the specified 
  1210.      * index. That is, the index returned is the largest value <i>k</i> 
  1211.      * such that:
  1212.      * <blockquote><pre>
  1213.      * this.charAt(k) == ch) && (k <= fromIndex)
  1214.      * </pre></blockquote>
  1215.      * is true.
  1216.      *
  1217.      * @param   ch          a character.
  1218.      * @param   fromIndex   the index to start the search from. There is no 
  1219.      *          restriction on the value of <code>fromIndex</code>. If it is 
  1220.      *          greater than or equal to the length of this string, it has 
  1221.      *          the same effect as if it were equal to one less than the 
  1222.      *          length of this string: this entire string may be searched. 
  1223.      *          If it is negative, it has the same effect as if it were -1: 
  1224.      *          -1 is returned.
  1225.      * @return  the index of the last occurrence of the character in the
  1226.      *          character sequence represented by this object that is less
  1227.      *          than or equal to <code>fromIndex</code>, or <code>-1</code>
  1228.      *          if the character does not occur before that point.
  1229.      */
  1230.     public int lastIndexOf(int ch, int fromIndex) {
  1231.     int min = offset;
  1232.     char v[] = value;
  1233.     
  1234.     for (int i = offset + ((fromIndex >= count) ? count - 1 : fromIndex) ; i >= min ; i--) {
  1235.         if (v[i] == ch) {
  1236.         return i - offset;
  1237.         }
  1238.     }
  1239.     return -1;
  1240.     }
  1241.  
  1242.     /**
  1243.      * Returns the index within this string of the first occurrence of the
  1244.      * specified substring. The integer returned is the smallest value 
  1245.      * <i>k</i> such that:
  1246.      * <blockquote><pre>
  1247.      * this.startsWith(str, <i>k</i>)
  1248.      * </pre></blockquote>
  1249.      * is <code>true</code>.
  1250.      *
  1251.      * @param   str   any string.
  1252.      * @return  if the string argument occurs as a substring within this
  1253.      *          object, then the index of the first character of the first
  1254.      *          such substring is returned; if it does not occur as a
  1255.      *          substring, <code>-1</code> is returned.
  1256.      * @exception java.lang.NullPointerException if <code>str</code> is 
  1257.      *          <code>null</code>.
  1258.      */
  1259.     public int indexOf(String str) {
  1260.     return indexOf(str, 0);
  1261.     }
  1262.  
  1263.     /**
  1264.      * Returns the index within this string of the first occurrence of the
  1265.      * specified substring, starting at the specified index. The integer 
  1266.      * returned is the smallest value <i>k</i> such that:
  1267.      * <blockquote><pre>
  1268.      * this.startsWith(str, <i>k</i>) && (<i>k</i> >= fromIndex)
  1269.      * </pre></blockquote>
  1270.      * is <code>true</code>.
  1271.      * <p>
  1272.      * There is no restriction on the value of <code>fromIndex</code>. If 
  1273.      * it is negative, it has the same effect as if it were zero: this entire 
  1274.      * string may be searched. If it is greater than the length of this 
  1275.      * string, it has the same effect as if it were equal to the length of 
  1276.      * this string: <code>-1</code> is returned.
  1277.      *
  1278.      * @param   str         the substring to search for.
  1279.      * @param   fromIndex   the index to start the search from.
  1280.      * @return  If the string argument occurs as a substring within this
  1281.      *          object at a starting index no smaller than
  1282.      *          <code>fromIndex</code>, then the index of the first character
  1283.      *          of the first such substring is returned. If it does not occur
  1284.      *          as a substring starting at <code>fromIndex</code> or beyond,
  1285.      *          <code>-1</code> is returned.
  1286.      * @exception java.lang.NullPointerException if <code>str</code> is 
  1287.      *          <code>null</code>
  1288.      */
  1289.     public int indexOf(String str, int fromIndex) {
  1290.         char v1[] = value;
  1291.         char v2[] = str.value;
  1292.         int max = offset + (count - str.count);
  1293.     if (fromIndex >= count) {
  1294.         if (count == 0 && fromIndex == 0 && str.count == 0) {
  1295.         /* There is an empty string at index 0 in an empty string. */
  1296.         return 0;
  1297.         }
  1298.         /* Note: fromIndex might be near -1>>>1 */
  1299.         return -1;
  1300.     }
  1301.         if (fromIndex < 0) {
  1302.             fromIndex = 0;
  1303.         }
  1304.     if (str.count == 0) {
  1305.         return fromIndex;
  1306.     }
  1307.  
  1308.         int strOffset = str.offset;
  1309.         char first  = v2[strOffset];
  1310.         int i = offset + fromIndex;
  1311.  
  1312.     startSearchForFirstChar:
  1313.         while (true) {
  1314.  
  1315.         /* Look for first character. */
  1316.         while (i <= max && v1[i] != first) {
  1317.         i++;
  1318.         }
  1319.         if (i > max) {
  1320.         return -1;
  1321.         }
  1322.  
  1323.         /* Found first character, now look at the rest of v2 */
  1324.         int j = i + 1;
  1325.         int end = j + str.count - 1;
  1326.         int k = strOffset + 1;
  1327.         while (j < end) {
  1328.         if (v1[j++] != v2[k++]) {
  1329.             i++;
  1330.             /* Look for str's first char again. */
  1331.             continue startSearchForFirstChar;
  1332.         }
  1333.         }
  1334.         return i - offset;    /* Found whole string. */
  1335.         }
  1336.     }
  1337.  
  1338.     /**
  1339.      * Returns the index within this string of the rightmost occurrence
  1340.      * of the specified substring.  The rightmost empty string "" is
  1341.      * considered to occur at the index value <code>this.length()</code>. 
  1342.      * The returned index is the largest value <i>k</i> such that 
  1343.      * <blockquote><pre>
  1344.      * this.startsWith(str, k)
  1345.      * </pre></blockquote>
  1346.      * is true.
  1347.      *
  1348.      * @param   str   the substring to search for.
  1349.      * @return  if the string argument occurs one or more times as a substring
  1350.      *          within this object, then the index of the first character of
  1351.      *          the last such substring is returned. If it does not occur as
  1352.      *          a substring, <code>-1</code> is returned.
  1353.      * @exception java.lang.NullPointerException  if <code>str</code> is 
  1354.      *          <code>null</code>.
  1355.      */
  1356.     public int lastIndexOf(String str) {
  1357.     return lastIndexOf(str, count);
  1358.     }
  1359.  
  1360.     /**
  1361.      * Returns the index within this string of the last occurrence of
  1362.      * the specified substring.
  1363.      * The returned index indicates the start of the substring, and it
  1364.      * must be equal to or less than <code>fromIndex</code>. That is, 
  1365.      * the index returned is the largest value <i>k</i> such that:
  1366.      * <blockquote><pre>
  1367.      * this.startsWith(str, k) && (k <= fromIndex)
  1368.      * </pre></blockquote>
  1369.      * 
  1370.      * @param   str         the substring to search for.
  1371.      * @param   fromIndex   the index to start the search from. There is no 
  1372.      *          restriction on the value of fromIndex. If it is greater than 
  1373.      *          the length of this string, it has the same effect as if it 
  1374.      *          were equal to the length of this string: this entire string 
  1375.      *          may be searched. If it is negative, it has the same effect 
  1376.      *          as if it were -1: -1 is returned.
  1377.      * @return  If the string argument occurs one or more times as a substring
  1378.      *          within this object at a starting index no greater than
  1379.      *          <code>fromIndex</code>, then the index of the first character of
  1380.      *          the last such substring is returned. If it does not occur as a
  1381.      *          substring starting at <code>fromIndex</code> or earlier,
  1382.      *          <code>-1</code> is returned.
  1383.      * @exception java.lang.NullPointerException if <code>str</code> is 
  1384.      *          <code>null</code>.
  1385.      */
  1386.     public int lastIndexOf(String str, int fromIndex) {
  1387.         /* 
  1388.      * Check arguments; return immediately where possible. For
  1389.      * consistency, don't check for null str.
  1390.      */
  1391.         int rightIndex = count - str.count;
  1392.     if (fromIndex < 0) {
  1393.         return -1;
  1394.     }
  1395.     if (fromIndex > rightIndex) {
  1396.         fromIndex = rightIndex;
  1397.     }
  1398.     /* Empty string always matches. */
  1399.     if (str.count == 0) {
  1400.         return fromIndex;
  1401.     }
  1402.  
  1403.     char v1[] = value;
  1404.     char v2[] = str.value;
  1405.     int strLastIndex = str.offset + str.count - 1;
  1406.     char strLastChar = v2[strLastIndex];
  1407.     int min = offset + str.count - 1;
  1408.     int i = min + fromIndex;
  1409.  
  1410.     startSearchForLastChar:
  1411.     while (true) {
  1412.  
  1413.         /* Look for the last character */
  1414.         while (i >= min && v1[i] != strLastChar) {
  1415.         i--;
  1416.         }
  1417.         if (i < min) {
  1418.         return -1;
  1419.         }
  1420.  
  1421.         /* Found last character, now look at the rest of v2. */
  1422.         int j = i - 1;
  1423.         int start = j - (str.count - 1);
  1424.         int k = strLastIndex - 1;
  1425.  
  1426.         while (j > start) {
  1427.             if (v1[j--] != v2[k--]) {
  1428.             i--;
  1429.             /* Look for str's last char again. */
  1430.             continue startSearchForLastChar;
  1431.         }
  1432.         }
  1433.  
  1434.         return start - offset + 1;    /* Found whole string. */
  1435.     }
  1436.     }
  1437.  
  1438.     /**
  1439.      * Returns a new string that is a substring of this string. The 
  1440.      * substring begins with the character at the specified index and 
  1441.      * extends to the end of this string. <p>
  1442.      * Examples:
  1443.      * <blockquote><pre>
  1444.      * "unhappy".substring(2) returns "happy"
  1445.      * "Harbison".substring(3) returns "bison"
  1446.      * "emptiness".substring(9) returns "" (an empty string)
  1447.      * </pre></blockquote>
  1448.      *
  1449.      * @param      beginIndex   the beginning index, inclusive.
  1450.      * @return     the specified substring.
  1451.      * @exception  IndexOutOfBoundsException  if 
  1452.      *             <code>beginIndex</code> is negative or larger than the 
  1453.      *             length of this <code>String</code> object.
  1454.      */
  1455.     public String substring(int beginIndex) {
  1456.     return substring(beginIndex, count);
  1457.     }
  1458.  
  1459.     /**
  1460.      * Returns a new string that is a substring of this string. The 
  1461.      * substring begins at the specified <code>beginIndex</code> and 
  1462.      * extends to the character at index <code>endIndex - 1</code>. 
  1463.      * Thus the length of the substring is <code>endIndex-beginIndex</code>.
  1464.      * <p>
  1465.      * Examples:
  1466.      * <blockquote><pre>
  1467.      * "hamburger".substring(4, 8) returns "urge"
  1468.      * "smiles".substring(1, 5) returns "mile"
  1469.      * </pre></blockquote>
  1470.      *
  1471.      * @param      beginIndex   the beginning index, inclusive.
  1472.      * @param      endIndex     the ending index, exclusive.
  1473.      * @return     the specified substring.
  1474.      * @exception  IndexOutOfBoundsException  if the
  1475.      *             <code>beginIndex</code> is negative, or 
  1476.      *             <code>endIndex</code> is larger than the length of 
  1477.      *             this <code>String</code> object, or 
  1478.      *             <code>beginIndex</code> is larger than 
  1479.      *             <code>endIndex</code>.
  1480.      */
  1481.     public String substring(int beginIndex, int endIndex) {
  1482.     if (beginIndex < 0) {
  1483.         throw new StringIndexOutOfBoundsException(beginIndex);
  1484.     } 
  1485.     if (endIndex > count) {
  1486.         throw new StringIndexOutOfBoundsException(endIndex);
  1487.     }
  1488.     if (beginIndex > endIndex) {
  1489.         throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
  1490.     }
  1491.     return ((beginIndex == 0) && (endIndex == count)) ? this :
  1492.         new String(offset + beginIndex, endIndex - beginIndex, value);
  1493.     }
  1494.  
  1495.     /**
  1496.      * Concatenates the specified string to the end of this string. 
  1497.      * <p>
  1498.      * If the length of the argument string is <code>0</code>, then this 
  1499.      * <code>String</code> object is returned. Otherwise, a new 
  1500.      * <code>String</code> object is created, representing a character 
  1501.      * sequence that is the concatenation of the character sequence 
  1502.      * represented by this <code>String</code> object and the character 
  1503.      * sequence represented by the argument string.<p>
  1504.      * Examples:
  1505.      * <blockquote><pre>
  1506.      * "cares".concat("s") returns "caress"
  1507.      * "to".concat("get").concat("her") returns "together"
  1508.      * </pre></blockquote>
  1509.      *
  1510.      * @param   str   the <code>String</code> that is concatenated to the end
  1511.      *                of this <code>String</code>.
  1512.      * @return  a string that represents the concatenation of this object's
  1513.      *          characters followed by the string argument's characters.
  1514.      * @exception java.lang.NullPointerException if <code>str</code> is 
  1515.      *          <code>null</code>.
  1516.      */
  1517.     public String concat(String str) {
  1518.     int otherLen = str.length();
  1519.     if (otherLen == 0) {
  1520.         return this;
  1521.     }
  1522.     char buf[] = new char[count + otherLen];
  1523.     getChars(0, count, buf, 0);
  1524.     str.getChars(0, otherLen, buf, count);
  1525.     return new String(0, count + otherLen, buf);
  1526.     }
  1527.  
  1528.     /**
  1529.      * Returns a new string resulting from replacing all occurrences of 
  1530.      * <code>oldChar</code> in this string with <code>newChar</code>. 
  1531.      * <p>
  1532.      * If the character <code>oldChar</code> does not occur in the 
  1533.      * character sequence represented by this <code>String</code> object, 
  1534.      * then a reference to this <code>String</code> object is returned. 
  1535.      * Otherwise, a new <code>String</code> object is created that 
  1536.      * represents a character sequence identical to the character sequence 
  1537.      * represented by this <code>String</code> object, except that every 
  1538.      * occurrence of <code>oldChar</code> is replaced by an occurrence
  1539.      * of <code>newChar</code>. 
  1540.      * <p>
  1541.      * Examples:
  1542.      * <blockquote><pre>
  1543.      * "mesquite in your cellar".replace('e', 'o')
  1544.      *         returns "mosquito in your collar"
  1545.      * "the war of baronets".replace('r', 'y')
  1546.      *         returns "the way of bayonets"
  1547.      * "sparring with a purple porpoise".replace('p', 't')
  1548.      *         returns "starring with a turtle tortoise"
  1549.      * "JonL".replace('q', 'x') returns "JonL" (no change)
  1550.      * </pre></blockquote>
  1551.      *
  1552.      * @param   oldChar   the old character.
  1553.      * @param   newChar   the new character.
  1554.      * @return  a string derived from this string by replacing every
  1555.      *          occurrence of <code>oldChar</code> with <code>newChar</code>.
  1556.      */
  1557.     public String replace(char oldChar, char newChar) {
  1558.     if (oldChar != newChar) {
  1559.         int len = count;
  1560.         int i = -1;
  1561.         char[] val = value; /* avoid getfield opcode */
  1562.         int off = offset;   /* avoid getfield opcode */
  1563.  
  1564.         while (++i < len) {
  1565.         if (val[off + i] == oldChar) {
  1566.             break;
  1567.         }
  1568.         }
  1569.         if (i < len) {
  1570.         char buf[] = new char[len];
  1571.         for (int j = 0 ; j < i ; j++) {
  1572.             buf[j] = val[off+j];
  1573.         }
  1574.         while (i < len) {
  1575.             char c = val[off + i];
  1576.             buf[i] = (c == oldChar) ? newChar : c;
  1577.             i++;
  1578.         }
  1579.         return new String(0, len, buf);
  1580.         }
  1581.     }
  1582.     return this;
  1583.     }
  1584.  
  1585.     /**
  1586.      * Converts all of the characters in this <code>String</code> to lower
  1587.      * case using the rules of the given <code>Locale</code>.
  1588.      * Usually, the characters are converted by calling 
  1589.      * <code>Character.toLowerCase</code>.  
  1590.      * Exceptions to this rule are listed in
  1591.      * the following table:
  1592.      * <p> </p>
  1593.      * <table border>
  1594.      * <tr>
  1595.      *   <th>Language Code of Locale</th>
  1596.      *   <th>Upper Case</th>
  1597.      *   <th>Lower Case</th>
  1598.      *   <th>Description</th>
  1599.      * </tr>
  1600.      * <tr>
  1601.      *   <td>tr (Turkish)</td>
  1602.      *   <td>\u0130</td>
  1603.      *   <td>\u0069</td>
  1604.      *   <td>capital letter I with dot above -> small letter i</td>
  1605.      * </tr>
  1606.      * <tr>
  1607.      *   <td>tr (Turkish)</td>
  1608.      *   <td>\u0049</td>
  1609.      *   <td>\u0131</td>
  1610.      *   <td>capital letter I -> small letter dotless i </td>
  1611.      * </tr>
  1612.      * </table>
  1613.      *
  1614.      * @param locale use the case transformation rules for this locale
  1615.      * @return the String, converted to lowercase.
  1616.      * @see     java.lang.Character#toLowerCase(char)
  1617.      * @see     java.lang.String#toUpperCase()
  1618.      * @since   JDK1.1
  1619.      */
  1620.     public String toLowerCase(Locale locale) {
  1621.         int     len        = count;
  1622.     int     off        = offset;
  1623.     char[]  val        = value;
  1624.     int     firstUpper;
  1625.     
  1626.     /* Now check if there are any characters that need to be changed. */
  1627.     scan: {
  1628.         for (firstUpper = 0 ; firstUpper < len ; firstUpper++) {
  1629.         char c = value[off+firstUpper];
  1630.         if (c != Character.toLowerCase(c)) break scan;
  1631.         }
  1632.         return this;
  1633.     }
  1634.  
  1635.         char[]  result = new char[count];
  1636.  
  1637.     /* Just copy the first few lowerCase characters. */
  1638.     System.arraycopy(val, off, result, 0, firstUpper);
  1639.     
  1640.         if (locale.getLanguage().equals("tr")) {
  1641.             // special loop for Turkey
  1642.         for (int i = firstUpper; i < len; ++i) {
  1643.                 char ch = val[off+i];
  1644.                 if (ch == 'I') {
  1645.                     result[i] = '\u0131'; // dotless small i
  1646.                     continue;
  1647.                 }
  1648.                 if (ch == '\u0130') {       // dotted I
  1649.                     result[i] = 'i';      // dotted i
  1650.                     continue;
  1651.                 }
  1652.                 result[i] = Character.toLowerCase(ch);
  1653.             }
  1654.         } else {
  1655.             // normal, fast loop
  1656.             for (int i = firstUpper; i < len; ++i) {
  1657.                 result[i] = Character.toLowerCase(val[off+i]);
  1658.             }
  1659.         }
  1660.         return new String(result);
  1661.     }
  1662.  
  1663.     /**
  1664.      * Converts all of the characters in this <code>String</code> to lower
  1665.      * case using the rules of the default locale, which is returned
  1666.      * by <code>Locale.getDefault</code>.
  1667.      * <p>
  1668.      * If no character in the string has a different lowercase version, 
  1669.      * based on calling the <code>toLowerCase</code> method defined by 
  1670.      * <code>Character</code>, then the original string is returned. 
  1671.      * <p>
  1672.      * Otherwise, this method creates a new <code>String</code> object that 
  1673.      * represents a character sequence identical in length to the character 
  1674.      * sequence represented by this String object, with every character 
  1675.      * equal to the result of applying the method 
  1676.      * <code>Character.toLowerCase</code> to the corresponding character of 
  1677.      * this <code>String</code> object. 
  1678.      * <p>Examples:
  1679.      * <blockquote><pre>
  1680.      * "French Fries".toLowerCase() returns "french fries"
  1681.      * "<img src="doc-files/capiota.gif"><img src="doc-files/capchi.gif"><img 
  1682.      * src="doc-files/captheta.gif"><img src="doc-files/capupsil.gif"><img 
  1683.      * src="doc-files/capsigma.gif">".toLowerCase() returns "<img 
  1684.      * src="doc-files/iota.gif"><img src="doc-files/chi.gif"><img 
  1685.      * src="doc-files/theta.gif"><img src="doc-files/upsilon.gif"><img 
  1686.      * src="doc-files/sigma1.gif">"
  1687.      * </pre></blockquote>
  1688.      *
  1689.      * @return  the string, converted to lowercase.
  1690.      * @see     java.lang.Character#toLowerCase(char)
  1691.      * @see     java.lang.String#toUpperCase()
  1692.      */
  1693.     public String toLowerCase() {
  1694.         return toLowerCase(Locale.getDefault());
  1695.     }
  1696.  
  1697.     /**
  1698.      * Converts all of the characters in this <code>String</code> to upper
  1699.      * case using the rules of the given locale.
  1700.      * Usually, the characters are converted by calling
  1701.      * <code>Character.toUpperCase</code>.  
  1702.      * Exceptions to this rule are listed in
  1703.      * the following table:
  1704.      * <p> </p>
  1705.      * <table border>
  1706.      * <tr>
  1707.      *   <th>Language Code of Locale</th>
  1708.      *   <th>Lower Case</th>
  1709.      *   <th>Upper Case</th>
  1710.      *   <th>Description</th>
  1711.      * </tr>
  1712.      * <tr>
  1713.      *   <td>tr (Turkish)</td>
  1714.      *   <td>\u0069</td>
  1715.      *   <td>\u0130</td>
  1716.      *   <td>small letter i -> capital letter I with dot above</td>
  1717.      * </tr>
  1718.      * <tr>
  1719.      *   <td>tr (Turkish)</td>
  1720.      *   <td>\u0131</td>
  1721.      *   <td>\u0049</td>
  1722.      *   <td>small letter dotless i -> capital letter I</td>
  1723.      * </tr>
  1724.      * <tr>
  1725.      *   <td>(all)</td>
  1726.      *   <td>\u00df</td>
  1727.      *   <td>\u0053 \u0053</td>
  1728.      *   <td>small letter sharp s -> two letters: SS</td>
  1729.      * </tr>
  1730.      * </table>
  1731.      * @param locale use the case transformation rules for this locale
  1732.      * @return the String, converted to uppercase.
  1733.      * @see     java.lang.Character#toUpperCase(char)
  1734.      * @see     java.lang.String#toLowerCase(char)
  1735.      * @since   JDK1.1
  1736.      */
  1737.     public String toUpperCase(Locale locale) {
  1738.     int     len        = count;
  1739.         int     off        = offset;
  1740.     char[]  val        = value;
  1741.     int     firstLower;
  1742.     
  1743.     /* Now check if there are any characters that need changing. */
  1744.     scan: {
  1745.         for (firstLower = 0 ; firstLower < len ; firstLower++) {
  1746.         char c = value[off+firstLower];
  1747.         if (c != Character.toUpperCase(c)) break scan;
  1748.         }
  1749.         return this;
  1750.     }
  1751.     
  1752.         char[]  result       = new char[len]; /* might grow! */
  1753.     int     resultOffset = 0;  /* result grows, so i+resultOffset
  1754.                     * is the write location in result */
  1755.     
  1756.     /* Just copy the first few upperCase characters. */
  1757.     System.arraycopy(val, off, result, 0, firstLower);
  1758.  
  1759.         if (locale.getLanguage().equals("tr")) {
  1760.             // special loop for Turkey
  1761.         for (int i = firstLower; i < len; ++i) {
  1762.                 char ch = val[off+i];
  1763.                 if (ch == 'i') {
  1764.             result[i+resultOffset] = '\u0130';  // dotted cap i
  1765.                     continue;
  1766.                 }
  1767.                 if (ch == '\u0131') {                   // dotless i
  1768.                     result[i+resultOffset] = 'I';       // cap I
  1769.                     continue;
  1770.                 }
  1771.                 if (ch == '\u00DF') {                   // sharp s
  1772.             /* Grow result. */
  1773.             char[] result2 = new char[result.length + 1];
  1774.             System.arraycopy(result, 0, result2, 0,
  1775.                      i + 1 + resultOffset);
  1776.                     result2[i+resultOffset] = 'S';
  1777.             resultOffset++;
  1778.             result2[i+resultOffset] = 'S';
  1779.             result = result2;
  1780.                     continue;
  1781.                 }
  1782.                 result[i+resultOffset] = Character.toUpperCase(ch);
  1783.             }
  1784.         } else {
  1785.             // normal, fast loop
  1786.             for (int i = firstLower; i < len; ++i) {
  1787.                 char ch = val[off+i];
  1788.                 if (ch == '\u00DF') { // sharp s
  1789.             /* Grow result. */
  1790.             char[] result2 = new char[result.length + 1];
  1791.             System.arraycopy(result, 0, result2, 0,
  1792.                      i + 1 + resultOffset);
  1793.                     result2[i+resultOffset] = 'S';
  1794.             resultOffset++;
  1795.             result2[i+resultOffset] = 'S';
  1796.             result = result2;
  1797.                     continue;
  1798.                 }
  1799.                 result[i+resultOffset] = Character.toUpperCase(ch);
  1800.             }
  1801.         }
  1802.         return new String(result);
  1803.     }
  1804.     
  1805.     /**
  1806.      * Converts all of the characters in this <code>String</code> to upper
  1807.      * case using the rules of the default locale, which is returned
  1808.      * by <code>Locale.getDefault</code>.
  1809.      *
  1810.      * <p>
  1811.      * If no character in this string has a different uppercase version, 
  1812.      * based on calling the <code>toUpperCase</code> method defined by 
  1813.      * <code>Character</code>, then the original string is returned. 
  1814.      * <p>
  1815.      * Otherwise, this method creates a new <code>String</code> object 
  1816.      * representing a character sequence identical in length to the 
  1817.      * character sequence represented by this <code>String</code> object and
  1818.      * with every character equal to the result of applying the method
  1819.      * <code>Character.toUpperCase</code> to the corresponding character of 
  1820.      * this <code>String</code> object. <p>
  1821.      * Examples:
  1822.      * <blockquote><pre>
  1823.      * "Fahrvergn¸gen".toUpperCase() returns "FAHRVERGN‹GEN"
  1824.      * "Visit Ljubinje!".toUpperCase() returns "VISIT LJUBINJE!"
  1825.      * </pre></blockquote>
  1826.      *
  1827.      * @return  the string, converted to uppercase.
  1828.      * @see     java.lang.Character#toUpperCase(char)
  1829.      * @see     java.lang.String#toLowerCase()
  1830.      */
  1831.     public String toUpperCase() {
  1832.         return toUpperCase(Locale.getDefault());
  1833.     }
  1834.  
  1835.     /**
  1836.      * Removes white space from both ends of this string. 
  1837.      * <p>
  1838.      * If this <code>String</code> object represents an empty character 
  1839.      * sequence, or the first and last characters of character sequence 
  1840.      * represented by this <code>String</code> object both have codes 
  1841.      * greater than <code>'\u0020'</code> (the space character), then a 
  1842.      * reference to this <code>String</code> object is returned. 
  1843.      * <p>
  1844.      * Otherwise, if there is no character with a code greater than 
  1845.      * <code>'\u0020'</code> in the string, then a new 
  1846.      * <code>String</code> object representing an empty string is created
  1847.      * and returned.
  1848.      * <p>
  1849.      * Otherwise, let <i>k</i> be the index of the first character in the 
  1850.      * string whose code is greater than <code>'\u0020'</code>, and let 
  1851.      * <i>m</i> be the index of the last character in the string whose code 
  1852.      * is greater than <code>'\u0020'</code>. A new <code>String</code> 
  1853.      * object is created, representing the substring of this string that 
  1854.      * begins with the character at index <i>k</i> and ends with the 
  1855.      * character at index <i>m</i>-that is, the result of 
  1856.      * <code>this.substring(<i>k</i>, <i>m</i>+1)</code>.
  1857.      * <p>
  1858.      * This method may be used to trim 
  1859.      * {@link Character#isSpace(char) whitespace} from the beginning and end 
  1860.      * of a string; in fact, it trims all ASCII control characters as well.
  1861.      *
  1862.      * @return  this string, with white space removed from the front and end.
  1863.      */
  1864.     public String trim() {
  1865.     int len = count;
  1866.     int st = 0;
  1867.     int off = offset;      /* avoid getfield opcode */
  1868.     char[] val = value;    /* avoid getfield opcode */
  1869.  
  1870.     while ((st < len) && (val[off + st] <= ' ')) {
  1871.         st++;
  1872.     }
  1873.     while ((st < len) && (val[off + len - 1] <= ' ')) {
  1874.         len--;
  1875.     }
  1876.     return ((st > 0) || (len < count)) ? substring(st, len) : this;
  1877.     }
  1878.  
  1879.     /**
  1880.      * This object (which is already a string!) is itself returned. 
  1881.      *
  1882.      * @return  the string itself.
  1883.      */
  1884.     public String toString() {
  1885.     return this;
  1886.     }
  1887.  
  1888.     /**
  1889.      * Converts this string to a new character array.
  1890.      *
  1891.      * @return  a newly allocated character array whose length is the length
  1892.      *          of this string and whose contents are initialized to contain
  1893.      *          the character sequence represented by this string.
  1894.      */
  1895.     public char[] toCharArray() {
  1896.     char result[] = new char[count];
  1897.     getChars(0, count, result, 0);
  1898.     return result;
  1899.     }
  1900.  
  1901.     /**
  1902.      * Returns the string representation of the <code>Object</code> argument. 
  1903.      *
  1904.      * @param   obj   an <code>Object</code>.
  1905.      * @return  if the argument is <code>null</code>, then a string equal to
  1906.      *          <code>"null"</code>; otherwise, the value of
  1907.      *          <code>obj.toString()</code> is returned.
  1908.      * @see     java.lang.Object#toString()  
  1909.      */
  1910.     public static String valueOf(Object obj) {
  1911.     return (obj == null) ? "null" : obj.toString();
  1912.     }
  1913.  
  1914.     /**
  1915.      * Returns the string representation of the <code>char</code> array
  1916.      * argument. The contents of the character array are copied; subsequent 
  1917.      * modification of the character array does not affect the newly 
  1918.      * created string. 
  1919.      *
  1920.      * @param   data   a <code>char</code> array.
  1921.      * @return  a newly allocated string representing the same sequence of
  1922.      *          characters contained in the character array argument.
  1923.      */
  1924.     public static String valueOf(char data[]) {
  1925.     return new String(data);
  1926.     }
  1927.  
  1928.     /**
  1929.      * Returns the string representation of a specific subarray of the 
  1930.      * <code>char</code> array argument. 
  1931.      * <p>
  1932.      * The <code>offset</code> argument is the index of the first 
  1933.      * character of the subarray. The <code>count</code> argument 
  1934.      * specifies the length of the subarray. The contents of the subarray 
  1935.      * are copied; subsequent modification of the character array does not 
  1936.      * affect the newly created string. 
  1937.      *
  1938.      * @param   data     the character array.
  1939.      * @param   offset   the initial offset into the value of the
  1940.      *                  <code>String</code>.
  1941.      * @param   count    the length of the value of the <code>String</code>.
  1942.      * @return  a newly allocated string representing the sequence of
  1943.      *          characters contained in the subarray of the character array
  1944.      *          argument.
  1945.      * @exception NullPointerException if <code>data</code> is 
  1946.      *          <code>null</code>.
  1947.      * @exception IndexOutOfBoundsException if <code>offset</code> is 
  1948.      *          negative, or <code>count</code> is negative, or 
  1949.      *          <code>offset+count</code> is larger than 
  1950.      *          <code>data.length</code>.
  1951.      */
  1952.     public static String valueOf(char data[], int offset, int count) {
  1953.     return new String(data, offset, count);
  1954.     }
  1955.     
  1956.     /**
  1957.      * Returns a String that is equivalent to the specified character array.
  1958.      * It creates a new array and copies the characters into it.
  1959.      *
  1960.      * @param   data     the character array.
  1961.      * @param   offset   initial offset of the subarray.
  1962.      * @param   count    length of the subarray.
  1963.      * @return  a <code>String</code> that contains the characters of the
  1964.      *          specified subarray of the character array.
  1965.      */
  1966.     public static String copyValueOf(char data[], int offset, int count) {
  1967.     // All public String constructors now copy the data.
  1968.     return new String(data, offset, count);
  1969.     }
  1970.  
  1971.     /**
  1972.      * Returns a String that is equivalent to the specified character array.
  1973.      * It creates a new array and copies the characters into it.
  1974.      *
  1975.      * @param   data   the character array.
  1976.      * @return  a <code>String</code> that contains the characters of the
  1977.      *          character array.
  1978.      */
  1979.     public static String copyValueOf(char data[]) {
  1980.     return copyValueOf(data, 0, data.length);
  1981.     }
  1982.  
  1983.     /**
  1984.      * Returns the string representation of the <code>boolean</code> argument. 
  1985.      *
  1986.      * @param   b   a <code>boolean</code>.
  1987.      * @return  if the argument is <code>true</code>, a string equal to
  1988.      *          <code>"true"</code> is returned; otherwise, a string equal to
  1989.      *          <code>"false"</code> is returned.
  1990.      */
  1991.     public static String valueOf(boolean b) {
  1992.     return b ? "true" : "false";
  1993.     }
  1994.  
  1995.     /**
  1996.      * Returns the string representation of the <code>char</code> 
  1997.      * argument. 
  1998.      *
  1999.      * @param   c   a <code>char</code>.
  2000.      * @return  a newly allocated string of length <code>1</code> containing
  2001.      *          as its single character the argument <code>c</code>.
  2002.      */
  2003.     public static String valueOf(char c) {
  2004.     char data[] = {c};
  2005.     return new String(0, 1, data);
  2006.     }
  2007.  
  2008.     /**
  2009.      * Returns the string representation of the <code>int</code> argument. 
  2010.      * <p>
  2011.      * The representation is exactly the one returned by the 
  2012.      * <code>Integer.toString</code> method of one argument. 
  2013.      *
  2014.      * @param   i   an <code>int</code>.
  2015.      * @return  a newly allocated string containing a string representation of
  2016.      *          the <code>int</code> argument.
  2017.      * @see     java.lang.Integer#toString(int, int)
  2018.      */
  2019.     public static String valueOf(int i) {
  2020.         return Integer.toString(i, 10);
  2021.     }
  2022.  
  2023.     /**
  2024.      * Returns the string representation of the <code>long</code> argument. 
  2025.      * <p>
  2026.      * The representation is exactly the one returned by the 
  2027.      * <code>Long.toString</code> method of one argument. 
  2028.      *
  2029.      * @param   l   a <code>long</code>.
  2030.      * @return  a newly allocated string containing a string representation of
  2031.      *          the <code>long</code> argument.
  2032.      * @see     java.lang.Long#toString(long)
  2033.      */
  2034.     public static String valueOf(long l) {
  2035.         return Long.toString(l, 10);
  2036.     }
  2037.  
  2038.     /**
  2039.      * Returns the string representation of the <code>float</code> argument. 
  2040.      * <p>
  2041.      * The representation is exactly the one returned by the 
  2042.      * <code>Float.toString</code> method of one argument. 
  2043.      *
  2044.      * @param   f   a <code>float</code>.
  2045.      * @return  a newly allocated string containing a string representation of
  2046.      *          the <code>float</code> argument.
  2047.      * @see     java.lang.Float#toString(float)
  2048.      */
  2049.     public static String valueOf(float f) {
  2050.     return Float.toString(f);
  2051.     }
  2052.  
  2053.     /**
  2054.      * Returns the string representation of the <code>double</code> argument. 
  2055.      * <p>
  2056.      * The representation is exactly the one returned by the 
  2057.      * <code>Double.toString</code> method of one argument. 
  2058.      *
  2059.      * @param   d   a <code>double</code>.
  2060.      * @return  a newly allocated string containing a string representation of
  2061.      *          the <code>double</code> argument.
  2062.      * @see     java.lang.Double#toString(double)
  2063.      */
  2064.     public static String valueOf(double d) {
  2065.     return Double.toString(d);
  2066.     }
  2067.  
  2068.     /**
  2069.      * Returns a canonical representation for the string object. 
  2070.      * <p>
  2071.      * A pool of strings, initially empty, is maintained privately by the 
  2072.      * class <code>String</code>. 
  2073.      * <p>
  2074.      * When the intern method is invoked, if the pool already contains a 
  2075.      * string equal to this <code>String</code> object as determined by 
  2076.      * the {@link #equals(Object)} method, then the string from the pool is 
  2077.      * returned. Otherwise, this <code>String</code> object is added to the 
  2078.      * pool and a reference to this <code>String</code> object is returned.
  2079.      * <p>
  2080.      * It follows that for any two strings <code>s</code> and <code>t</code>, 
  2081.      * <code>s.intern() == t.intern()</code> is <code>true</code> 
  2082.      * if and only if <code>s.equals(t)</code> is <code>true</code>.
  2083.      * <p>
  2084.      * All literal strings and string-valued constant expressions are 
  2085.      * interned. String literals are defined in ß3.10.5 of the 
  2086.      * <a href="http://java.sun.com/docs/books/jls/html/">Java Language 
  2087.      * Specification</a>
  2088.      *
  2089.      * @return  a string that has the same contents as this string, but is
  2090.      *          guaranteed to be from a pool of unique strings.
  2091.      */
  2092.     public native String intern();
  2093.  
  2094. }
  2095.